﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Store;
using AMM.Model;
using AMM.Model.Store;

namespace AMM.Business.Store {
    public class OutsourceOrderProcessor : BaseProcessor {

        private int _pageSize;
        private OutsourceOrderRepository _outsourceOrderRepository;

        public OutsourceOrderProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._outsourceOrderRepository = new OutsourceOrderRepository(pageSize);
        }

        public OutsourceOrder FetchOutsourceOrderById(int outsourceOrderId) {
            try {
                return this._outsourceOrderRepository.FetchOutsourceOrderById(outsourceOrderId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定委外订单失败！");
            }
        }

        public OutsourceOrder FetchOutsourceOrderByProductConductId(int productConductId) {
            try {
                return this._outsourceOrderRepository.FetchOutsourceOrderByProductConductId(productConductId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定委外订单失败！");
            }
        }
        public List<OutsourceOrderDetailsViewModel> FetchOutsourceOrderDetailsViewModelList(int outsourceOrderId) {
            try {
                return this._outsourceOrderRepository.FetchOutsourceOrderByIdForView(outsourceOrderId).OutsourceOrderDetails.Select(x => {
                    return new OutsourceOrderDetailsViewModel() {
                        Key = x.OutsourceOrderDetailsId,
                        ProduceConductCode = x.ProduceConduct.Code,
                        DeliveryDate = x.DeliveryDate,
                        InStoreQuantity = x.InStoreQuantity,
                        ProductCode = x.ProduceConduct.Product.Code,
                        Quantity = x.Quantity,
                        Price = x.Price,
                        Total = x.Total.Value,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定委外订单失败！");
            }
        }

        public List<OutsourceOrderViewModel> FetchOutsourceOrderViewModelList(OutsourceOrderSearchModel searchModel, int pageIndex = 1) {
            try {
                List<OutsourceOrder> resultList = this._outsourceOrderRepository.FetchOutsourceOrderList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new OutsourceOrderViewModel() {
                        Key = x.OutsourceOrderId,
                        Code = x.Code,
                        IsPrinted = x.IsPrinted,
                        PrintedDatetime = x.PrintedDate,
                        VendorCode = x.Vendor.Code,
                        VendorName = x.Vendor.Name,
                        OrderStatus = MethodHelper.GetEnumsDescription(typeof(AuditStatus), x.Status),
                        AuditedBy = x.AuditedBy,
                        AuditedDatetime = x.AuditedDatetime,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取委外订单列表失败！");
            }
        }

        public void Insert(OutsourceOrder outsourceOrder) {
            try {
                outsourceOrder.OutsourceOrderId = Guid.NewGuid().GetHashCode();
                outsourceOrder.CreatedDatetime = DateTime.Now;
                outsourceOrder.Status = (int)AuditStatus.Waiting;
                outsourceOrder.OutsourceOrderDetails.ToList().ForEach(x => {
                    x.CreatedBy = outsourceOrder.CreatedBy;
                    x.CreatedDatetime = outsourceOrder.CreatedDatetime;
                    x.OutsourceOrderDetailsId = Guid.NewGuid().GetHashCode();
                });
                this._outsourceOrderRepository.Insert(outsourceOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新委外订单失败！");
            }
        }

        public void Update(OutsourceOrder outsourceOrder) {
            try {
                OutsourceOrder oldOutsourceOrder = this.FetchOutsourceOrderById(outsourceOrder.OutsourceOrderId);
                if (!oldOutsourceOrder.RowVersion.SequenceEqual(outsourceOrder.RowVersion)) {
                    throw this.BuildException("更新委外订单失败，数据不一致性错误！");
                }
                outsourceOrder.ModifiedDatetime = DateTime.Now;
                this._outsourceOrderRepository.UpdateOutsourceOrder(outsourceOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新委外订单失败！");
            }
        }

        public void Delete(OutsourceOrder outsourceOrder) {
            try {
                this._outsourceOrderRepository.Delete(outsourceOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除委外订单失败！");
            }
        }

        public void Audit(OutsourceOrder outsourceOrder) {
            try {
                OutsourceOrder oldOutsourceOrder = this.FetchOutsourceOrderById(outsourceOrder.OutsourceOrderId);
                if (!oldOutsourceOrder.RowVersion.SequenceEqual(outsourceOrder.RowVersion)) {
                    throw this.BuildException("审核委外订单失败，数据不一致性错误！");
                }
                outsourceOrder.AuditedDatetime = DateTime.Now;
                this._outsourceOrderRepository.UpdateOutsourceOrder(outsourceOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("审核委外订单失败！");
            }
        }

        public void Delete(params int[] outsourceOrderIds) {
            try {
                this._outsourceOrderRepository.DeleteMultipleOutsourceOrder(outsourceOrderIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除委外订单失败！");
            }
        }

        public int GetTheMaxFlowNumber(string startString) {
            try {
                return this._outsourceOrderRepository.GetTheMaxFlowNumber(startString) + 1;
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取委外订单最大流水号失败！");
            }
        }
    }
}
